<template>
	<view>
		<input :disabled="disabled" class="uni-input" type="text" v-model="email" placeholder="请输入你要绑定的邮箱"/>
		<view class="py-2 px-3">
			<button class="bg-main text-white" style="border-radius: 50rpx;border: 0;" type="primary" :disabled="disabled" :class="{'bg-main-disabled':disabled}" @click="submit">绑定</button>
		</view>
	</view>
</template>

<script>
	import {getEmailAdd} from "@/network/login.js";
	export default {
		data() {
			return {
				email:""
			}
		},
		computed: {
			disabled() {
				return this.$store.state.userInfo?.email ?? false
			}
		},
		onLoad() {
			this.email = this.$store.state.userInfo?.email ?? ""
		},
		methods: {
			check(){
				// 正则邮箱格式
				let rule = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if (!rule.test(this.email)) {
					uni.showToast({
						title:"邮箱格式不正确",
						icon:"none"
					})
					return false;
				}
				return true
			},
			submit(){
				if (!this.check()) {
					return;
				}
				let data = {
					email:this.email
				}
				getEmailAdd(data).then(attr=>{
					let [err,res] = attr
					console.log(res)
					if(res.data.status){
						this.$store.state.useInfo['email'] = res.data.msg
						uni.navigateBack({
							delta: 1
						});
					}else{
						console.log(res.data)
					}
					
				})
				
			}
		}
	}
</script>

<style>

</style>
